/*
 * ScriptPath.java
 *
 * Created on December 30, 2007, 1:36 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package filecopier.script;

import java.io.File;

/**
 *
 * @author david
 */
public class ScriptPath {

    private String key = null;
    private String regex = null;
    private String defaultPath = null;
    private String[] pathArray = null;
    private int limit = 0;

    /** Creates a new instance of ScriptPath */
    public ScriptPath(final String key, final String regex) {
        setKey(key);
        setRegex(regex);
        setPathArray();
    }

    public ScriptPath(final String key, final String regex, int limit) {
        this(key, regex);
        setLimit(limit);
    }

    public ScriptPath(final String key, final String regex, final String defaultValue) {
        this(key, regex);
        setDefaultPath(defaultValue);
    }

    public ScriptPath(final String key, final String regex, final String defaultValue, final String path) {
        setKey(key);
        setRegex(regex);
        setDefaultPath(defaultValue);
        setPath(path);
        setPathArray();
    }

    public ScriptPath(final String key, final String regex, int limit, final String defaultValue) {
        this(key, regex, limit);
        setDefaultPath(defaultValue);
    }

    public ScriptPath(final String key, final String regex, int limit, final String defaultValue, final String path) {
        setKey(key);
        setRegex(regex);
        setDefaultPath(defaultValue);
        setLimit(limit);
        setPath(path);
        setPathArray();
    }

    public void setKey(final String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public void setRegex(final String regex) {
        this.regex = regex;
    }

    public String getRegex() {
        return regex;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getLimit() {
        return limit;
    }

    public void setDefaultPath(final String defaultPath) {
        this.defaultPath = defaultPath;
    }

    public String getDefaultPath() {
        return defaultPath;
    }

    public void setPathToDefault() {
        System.setProperty(key, defaultPath);
        setPathArray();
    }

    public void setPath(final String path) {
        System.setProperty(key, path != null && !path.isEmpty() ? path : defaultPath);
        setPathArray();
    }

    private void setPath() {
        StringBuilder pathBuilder = new StringBuilder();
        for (String s : pathArray) {
            pathBuilder.append(s).append(regex);
        }
        setPath(pathBuilder.substring(0, pathBuilder.lastIndexOf(regex)));
    }

    public String getPath() {
        return (System.getProperty(key, defaultPath));
    }

    private void setPathArray() {
        String path = getPath();
        if (path != null) {
            pathArray = path.split(regex, limit);
        }
    }

    public String[] getPathArray() {
        return pathArray;
    }

    public int getPathEntryIndex(final String pathEntry) {
        int idx = -1;
        int i = 0;
        for (String s : pathArray) {
            if (pathEntry.equals(s)) {
                idx = i;
                break;
            }
            i++;
        }
        return idx;
    }

    public String getPathEntryValue(int idx) {
        return (idx < pathArray.length ? pathArray[idx] : null);
    }

    public void reorderPath(int startIdx, int endIdx) {
        String pathPromote = pathArray[endIdx];
        for (int i = endIdx; i > startIdx; i--) {
            pathArray[i] = pathArray[i - 1];
        }
        pathArray[startIdx] = pathPromote;
        setPath();
    }

    public void reorderPath(final String startString, final String endString) {
        int startIdx = getPathEntryIndex(startString);
        int endIdx = getPathEntryIndex(endString);
        if (startIdx > -1 && endIdx > -1) {
            reorderPath(startIdx, endIdx);
        }
    }

    // Returns the full path of the file if it exists in one of the paths in the path list or null if it does not exist.
    public File getFile(final String fileName) {
        File file = null;
        String fileSep = System.getProperty("file.separator");

        for (String dir : pathArray) {
            File f = new File(dir + (dir.endsWith(fileSep) ? "" : fileSep) + fileName);
            if (f.exists()) {
                file = f;
                break;
            }
        }
        return file;
    }

    // Returns the full path of the file if it exists in one of the paths in the path list in the form of a String or null
    // if it does not exist.
    public String getFilePath(final String fileName) {
        File file = getFile(fileName);
        return (file != null ? file.getPath() : null);
    }
}
